package com.niu.wm.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;

import com.niu.wm.model.Customer;
import com.niu.wm.service.BaseService;
import com.niu.wm.service.MailService;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailServiceImpl extends BaseService implements MailService {

	public static final String DEFAULT_ENCODING = "utf-8";
	private String smtp = "smtp.163.com";

	@Override
	public void test() {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		Properties props = new Properties();
		props.put("mail.smtp.auth", Boolean.TRUE);
		/*
		  props.put("mail.smtp.ssl.enable", true);
		  props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
		  props.put("mail.smtp.timeout", 25000);
		 */
		sender.setHost(smtp);
		sender.setJavaMailProperties(props);

		sender.setUsername("junseen2018");
		// 登录密码
		// sender.setPassword("Junseen1234");
		// 授权码
		sender.setPassword("J12345");

		// 建立邮件讯息
		SimpleMailMessage mailMessage = new SimpleMailMessage();

		// 设定收件人、寄件人、主题与内文
		mailMessage.setTo("2599403@qq.com");
		mailMessage.setCc("wangxjemail@163.com");
		mailMessage.setFrom("junseen2018@163.com");

		mailMessage.setSubject("Test from junseen");
		mailMessage.setText("This is a test!!!");

		// 传送邮件
		sender.send(mailMessage);

		System.out.println("邮件传送OK..");
	}

	// 报价信息
	@Override
	public void sendPriceMail(String title, String text, List<Customer> ls,
			File[] attaches) {
		JavaMailSenderImpl mailSender = null;
		try {
			mailSender = new JavaMailSenderImpl();
			Properties props = new Properties();
			props.put("mail.smtp.auth", Boolean.TRUE);
			
			props.put("mail.smtp.ssl.enable", true);
			props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
			props.put("mail.smtp.timeout", 25000);
			  
			mailSender.setHost("smtp.163.com");
			mailSender.setJavaMailProperties(props);
			// 登录密码
			mailSender.setPassword("Junseen1234");
			// 授权码
			mailSender.setPassword("J12345");

			MimeMessage msg = mailSender.createMimeMessage();
			// 生成spring的helper类，赋值收件人、发件人、抄送、主题等
			MimeMessageHelper helper = new MimeMessageHelper(msg, true,
					DEFAULT_ENCODING);

			// 收件人
			helper.setTo("2599403@qq.com");
			helper.setCc("wangxjemail@163.com");
			helper.setFrom("junseen2018@163.com");
			// 主题
			helper.setSubject(title);

			// 使用Freemarker生成html格式内容，即mailTemplate.ftl的内容
			String content = text;
			helper.setText(content, true);

			// 附件对象，添加进邮件helper内
			for (File file : attaches) {
				String fileName = file.getName();
				helper.addAttachment(fileName, file);
			}

			mailSender.send(msg);
		} catch (MessagingException e) {
			getLogger().error("构造邮件失败", e);
		} catch (Exception e) {
			getLogger().error("发送邮件失败", e);
		}
	}

	public static void main(String[] args) {
		
		testSimple();
		
		String title="商品报价";
		String text="dear ";
		List<Customer> ls = new ArrayList<>();
		File[] files = new File[2];
		files[0] = new java.io.File("f:/test1.jpg");
		files[1] = new java.io.File("f:/test2.jpg");
		
		priceMail(title, text, ls, files);
	}
	
	public static void priceMail(String title, String text, List<Customer> ls,
			File[] attaches) {
		JavaMailSenderImpl mailSender = null;
		try {
			mailSender = new JavaMailSenderImpl();
			Properties props = new Properties();
			props.put("mail.smtp.auth", Boolean.TRUE);
			mailSender.setHost("smtp.163.com");
			mailSender.setJavaMailProperties(props);
			// 登录密码
			mailSender.setPassword("Junseen1234");
			// 授权码
			mailSender.setPassword("J12345");

			MimeMessage msg = mailSender.createMimeMessage();
			// 生成spring的helper类，赋值收件人、发件人、抄送、主题等
			MimeMessageHelper helper = new MimeMessageHelper(msg, true,
					DEFAULT_ENCODING);

			// 收件人
			helper.setTo("2599403@qq.com");
			helper.setCc("wangxjemail@163.com");
			helper.setFrom("junseen2018@163.com");
			// 主题
			helper.setSubject(title);

			// 使用Freemarker生成html格式内容，即mailTemplate.ftl的内容
			String content = text;
			helper.setText(content, true);
/*
			// 附件对象，添加进邮件helper内
			for (File file : attaches) {
				String fileName = file.getName();
				helper.addAttachment(fileName, file);
			}
*/			

			mailSender.send(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void testSimple() {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		Properties props = new Properties();
		props.put("mail.smtp.auth", Boolean.TRUE);
		
		  props.put("mail.smtp.ssl.enable", true);
		  props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
		  props.put("mail.smtp.timeout", 25000);
		/* 
		sender.setHost("smtp.163.com");
		sender.setJavaMailProperties(props);
		sender.setUsername("junseen2018");
		// 登录密码
		sender.setPassword("Junseen1234");
		// 授权码
		sender.setPassword("J12345");
		*/
		
		sender.setHost("smtp.qq.com");
		sender.setJavaMailProperties(props);
		sender.setUsername("junseen2018");
		// 授权码
		sender.setPassword("mqtsmiemcqawbhbg");
		

		// 建立邮件讯息
		SimpleMailMessage mailMessage = new SimpleMailMessage();

		// 设定收件人、寄件人、主题与内文
		mailMessage.setTo("2599403@qq.com");
		mailMessage.setCc("wangxjemail@163.com");
		//mailMessage.setFrom("junseen2018@163.com");
		mailMessage.setFrom("1665855866@qq.com");
		

		mailMessage.setSubject("From junseen2018");
		mailMessage.setText("mail text");

		// 传送邮件
		sender.send(mailMessage);

		System.out.println("邮件传送OK..");
	}

}
